package edu.uwlax.cs.oayonlinestore.client.gui.shop;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.Order;
import edu.uwlax.cs.oayonlinestore.client.User;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.ProductDetailsVO;

public class PageSubCategory extends TitlePage {

	private class ProductTable extends DekoTable {

		private class AddToCartAction extends DefaultAsyncCallback implements
				ClickListener {

			private TextBox ammount;

			private int productID;

			public AddToCartAction(int productID, TextBox ammount) {
				super();
				this.productID = productID;
				this.ammount = ammount;
			}

			public void onClick(Widget arg0) {
				try {
					int am = Integer.parseInt(ammount.getText());
					int id = Order.get().getId();
					OnlinestoreRService.Util.getInstance().addProductToOrder(
							productID, am, id, this);
				} catch (NumberFormatException e) {
					Window.alert("'" + ammount.getText()
							+ "' is not a valid number.");
				}
			}

			public void onSuccess(Object arg0) {
				Order.get().update();
			}

		}

		public ProductTable() {
			String disc = (User.get().isIsIndustrialCustomer()) ? "Industrial"
					: "Normal";
			addTitle("Brand");
			addTitle("Product");
			addTitle("Size");
			addTitleHTML(disc + "<br>Discount");
			addTitle("Price");
			addTitle("add to Cart");
		}

		public void addItem(ProductDetailsVO p) {
			AddRow r = addRow();
			r.add(p.getBrand());
			r.add(p.getName());
			r.add(p.getSize());
			if (User.get().isIsIndustrialCustomer())
				r.add(p.getIndustrialDiscount() + "%").setHAlign(r.RIGHT);
			else
				r.add(p.getNormalDiscount() + "%").setHAlign(r.RIGHT);
			r.add(p.getPrice(), "USD");

			HorizontalPanel addcart = new HorizontalPanel();
			TextBox ammount = new TextBox();
			ammount.setWidth("32px");
			ammount.setTextAlignment(TextBox.ALIGN_RIGHT);
			ammount.setText("1");
			addcart.add(ammount);
			addcart.add(new Button("add", new AddToCartAction(p.getId(),
					ammount)));

			r.add(addcart);
		}
	}

	private ProductTable products;

	protected void create() {
		setTitle("Sub Category");
		add(products = new ProductTable());
	}

	public String getID() {
		return "Subcategory";
	}

	protected void onDataArrived(Object o) {
		products.cleanExceptHeader();
		ProductDetailsVO[] prod = (ProductDetailsVO[]) o;
		for (int i = 0; i < prod.length; i++)
			products.addItem(prod[i]);

	}

	protected boolean onShow(int param) {
		OnlinestoreRService.Util.getInstance().getProductDetailList(param,
				getAsyncHandler());
		return true;
	}

}
